<template>
  <div>
    <x-header :left-options="{showBack: true,preventGoBack:true}"
              @on-click-back="goBack" theme="" class="queryHeader">新增特约
    </x-header>
    <div class="weui-cells__title">阅读并选择方案所需特约</div>
    <checklist :options="appoints" :value="hasAppoints" @on-change="changeLists"></checklist>
    <box gap="10px 10px">
      <x-button type="primary" class="positiveBtn" @click.native="onSave">保存
      </x-button>
    </box>
  </div>
</template>
<script>
  import {XHeader, Group, Checklist, Box, XButton} from 'Vux'
  export default {
    components: {
      XHeader, Group, Checklist, Box, XButton
    },
    data(){
      return {
        appoints: [],
        ios: false,
        hasAppoints: []
      }
    },
    methods: {
      //返回到方案信息页面
      goBack(){
        this.$router.push('/addScheme');
        this.$store.commit('CHANGE_TRANSITION', 'right');
        this.$store.commit('SAVE_SPECIAL_APPOINTS', []);
      },
      //变更Lists
      changeLists(value){
        let val = [];
        if (value.indexOf(1) >= 0 && value.indexOf(1.03) >= 0) {
          if (value.indexOf(0.95) >= 0) {
            val = [value[value.length - 1], 0.95];
          } else {
            val = [value[value.length - 1]];
          }
        } else {
          val = value
        }
        let rule = (a, b) => Number(a) - Number(b);
        this.hasAppoints = val.sort(rule);
        console.log(this.hasAppoints)
      },
      //equals
      equals(ary1, ary2){
        if (!ary2) {
          return false;
        }
        if (ary1.length !== ary2.length)
          return false;
        for (var i = 0, l = ary1.length; i < l; i++) {
          if (ary1[i] instanceof Array && ary2[i] instanceof Array) {
            if (!ary1[i].equals(ary2[i]))
              return false;
          }
          else if (ary1[i] !== ary2[i]) {
            return false;
          }
        }
        return true;
      },
      //保存特约
      onSave(){
        let appoints = this.$store.state.app.appoints;
        if (this.hasAppoints.length > 0) {
          if (this.equals(appoints, this.hasAppoints)) {
            this.$router.push('/addScheme');
          } else {
            let thus = this;
            this.$vux.confirm.show({
              theme: thus.ios ? 'ios' : 'android',
              title: '提示',
              content: '特约选择会影响责任信息',
              confirmText: '继续',
              cancelText: '取消',
              onCancel () {
                thus.$vux.confirm.hide()
              },
              onConfirm () {
                thus.$store.commit('REMOVE_INSURANCE');
                thus.$store.commit('SAVE_SPECIAL_APPOINTS', thus.hasAppoints);
                thus.$router.push('/addScheme');
              }
            })
          }
        } else {
          this.$store.commit('SAVE_SPECIAL_APPOINTS', thus.hasAppoints);
          this.$router.push('/addScheme');
        }
        this.$store.commit('CHANGE_TRANSITION', 'right')
      }
    },
    mounted(){
      let appoints = this.$store.state.app.appoints;
      let allAppoints = this.$store.state.app.allAppoints;
      if (appoints.length > 0) {
        this.hasAppoints = appoints
      }
      if (allAppoints) {
        this.appoints = allAppoints
      }
      this.ios = this.$store.state.app.ios;
    }
  }
</script>
